'use strict'

import { app, BrowserWindow, ipcMain } from 'electron'

const isDev = process.env.NODE_ENV === 'development'

const test = require(`../../build/${isDev ? 'Debug' : 'Release'}/wmp.node`)

ipcMain.on('add-request', (event, arg1, arg2) => {
  const startUpResult = test.startUp(144115211585659690, 1400115281, false)
  console.log('start up result = ' + startUpResult)
  if (startUpResult === 0) {
    let frameNum = 0
    setTimeout(() => {
      let ret = test.preview('', true, (size, data, format, width, height, rotate, uin, stream) => {
        // console.log(size, data, format, width, height, rotate, uin, stream)
        event.sender.send('add-reply', data, ++frameNum, width, height)
      })
      console.log('preview ret:', ret)
    }, 500)
  }
})

/**
 * Set `__static` path to static files in production
 * https://simulatedgreg.gitbooks.io/electron-vue/content/en/using-static-assets.html
 */
if (process.env.NODE_ENV !== 'development') {
  global.__static = require('path').join(__dirname, '/static').replace(/\\/g, '\\\\')
}

let mainWindow
const winURL = process.env.NODE_ENV === 'development'
  ? `http://localhost:9080`
  : `file://${__dirname}/index.html`

function createWindow () {
  const {dialog} = require('electron')
  // const electron = require('electron')
  // console.log('hello, electron:', electron)
  // let s = ''
  // for (let k in electron) {
  //   s += k.toString() + ', '
  // }

  // dialog.showMessageBox(null, {'message': 'hello, canvas 13+8:' + (test.add(13, 8)).toString()}, null)

  console.log('hello ' + test.add(1, 2))

  // const protocol = electron.protocol
  // handles links `wemeet://<something>`
  const PROTOCOL_PREFIX = 'wemeet'
  // protocol.registerHttpProtocol(PROTOCOL_PREFIX, (req, cb) => {
  //   dialog.showMessageBox(null, {'message': 'hello'}, null)
  //   console.log('wemeet scheme triggered:', req)
  //   alert('wemeet scheme triggered:', req)
  //   dialog.showMessageBox(null, {'message': req.toString()}, null)
  //   cb(null, 'ok')
  // })
  app.setAsDefaultProtocolClient(PROTOCOL_PREFIX)
  app.on('open-url', (event, url) => {
    dialog.showMessageBox(null, {'message': 'welcome, you are from:' + url.toString()}, null)
    // let m = url.match(/join\?meetingcode=(\d+)/)
    // if (m) {
    //   window.meetingCodeToJoin = m[1]
    // }
  })

  /**
   * Initial window options
   */
  mainWindow = new BrowserWindow({
    height: 563,
    useContentSize: true,
    width: 1000
  })

  mainWindow.loadURL(winURL)

  mainWindow.on('closed', () => {
    mainWindow = null
  })
}

app.on('ready', createWindow)

app.on('window-all-closed', () => {
  if (process.platform !== 'darwin') {
    app.quit()
  }
})

app.on('activate', () => {
  if (mainWindow === null) {
    createWindow()
  }
})

/**
 * Auto Updater
 *
 * Uncomment the following code below and install `electron-updater` to
 * support auto updating. Code Signing with a valid certificate is required.
 * https://simulatedgreg.gitbooks.io/electron-vue/content/en/using-electron-builder.html#auto-updating
 */

/*
import { autoUpdater } from 'electron-updater'

autoUpdater.on('update-downloaded', () => {
  autoUpdater.quitAndInstall()
})

app.on('ready', () => {
  if (process.env.NODE_ENV === 'production') autoUpdater.checkForUpdates()
})
 */
